package top.roadvast.rabbitmq.provider;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import top.roadvast.rabbitmq.utils.RabbitmqUtil;

import java.io.IOException;
import java.util.Scanner;

/**
 * @Author 郭泽鹏
 * @Date 2021/12/26 10:36
 * @Version 1.0
 * @Description rabbitmq六种工作模式之一：工作模式
 */
@SpringBootTest
public class WorkProvider {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitmqUtil.getConnection();
        final Channel channel = connection.createChannel();
        //参数：queue,durable,exclusive,autoDelete,arguments
//        channel.queueDeclare("workqueue", false, false, false, null);
        //队列持久化：由于rabbitmq对已经存在的队列不允许修改参数，所以定义新的队列
        channel.queueDeclare("task_queue", true, false, false, null);
        while(true) {
            //控制台输入的消息发送到rabbitmq
            System.out.println("输入消息：");
            String msg = new Scanner(System.in).nextLine();
            //如果输入的是"exit"则结束生产者进程
            if ("exit".equalsIgnoreCase(msg)) {
                break;
            }
            //参数：exchange,routingKey,props,body
//            channel.basicPublish("", "workqueue", null, msg.getBytes());
            //消息持久化：MessageProperties.PERSISTENT_TEXT_PLAIN
            channel.basicPublish("", "workqueue", MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
            System.out.println("已发送消息：" + msg);
        }
        RabbitmqUtil.closeConnection(connection, channel);
    }
}
